package com.cn.crmsystem.service.systemService.impl;

import com.cn.crmsystem.mapper.systemMapper.RoleMapper;
import com.cn.crmsystem.pojo.Login;
import com.cn.crmsystem.pojo.ResultJson;
import com.cn.crmsystem.pojo.Role;
import com.cn.crmsystem.pojo.User;
import com.cn.crmsystem.service.systemService.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.sound.midi.Soundbank;
import java.util.HashMap;
import java.util.List;

@Service
public class RoleServiceImpl implements RoleService{

    @Autowired
    private RoleMapper roleMapper;


    @Override
    public ResultJson selectRole(Role role) {

        List<Role> list = roleMapper.selectRole(role);
        if (list != null && list.size() > 0){
            return new ResultJson().getSuccess("查询成功",list);
        }
        return new ResultJson().getFail("查询失败");
    }

    @Override
    public ResultJson addRole(Role role, Login login) {
        HashMap<Object, Object> map = new HashMap<>();
        map.put("role",role);
        map.put("login",login);
        if (roleMapper.addRole(map) > 0) {
            return new ResultJson().getSuccess("新增成功");
        }
        return new ResultJson().getFail("新增失败");
    }

    @Override
    public ResultJson deleteRole(Integer id) {
        int rows = roleMapper.deleteRole(id);
        if (rows > 0){
            return new ResultJson().getSuccess("删除成功");
        }
        return new ResultJson().getFail("删除失败");
    }

    @Override
    public ResultJson updateRole(Role role, Login login) {
        HashMap<Object, Object> map = new HashMap<>();
        map.put("role",role);
        map.put("login",login);
        if (roleMapper.updateRole(map) > 0) {
            return new ResultJson().getSuccess("修改成功");
        }
        return new ResultJson().getFail("修改失败");
    }

    @Override
    public Role getRole(Integer user_id) {
        return roleMapper.getRole(user_id);
    }

    @Override
    public ResultJson deleteManyRole(Integer[] ids) {
        int result=0;
        for(int i=0;i<ids.length;i++){
            result=roleMapper.deleteRole(ids[i]);
        }
        if(result>0){
            return new ResultJson<>().getSuccess("删除成功");
        }else{
            return new ResultJson<>().getSuccess("删除失败");
        }
    }
}
